<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>条件式属性访问</title>
</head>

<body>
  <script>
    let p = { a: { x: 1 }, b: { x: 1, y: 2 } }
    console.log(p.a.x.d?.f);//？.可选链操作符

    let a = { b: {} }
    console.log(a.b.c?.d);
    console.log(a.b.c?.['d']);
    // 条件式属性访问
    let b
    let index = 0
    console.log(b);
    // console.log(b[index]); 报typeError
    console.log(b?.[index]);
    console.log(a.b[index++]?.c);

    let d = [a,null,undefined]
    console.log(d[4]);

  </script>
</body>

</html>